//
//  KTThumbsViewController.m
//  KTPhotoBrowser
//
//  Created by Kirby Turner on 2/3/10.
//  Copyright 2010 White Peak Software Inc. All rights reserved.
//

#import "KTThumbsViewController.h"
#import "KTThumbsView.h"
#import "KTThumbView.h"
#import "KTPhotoScrollViewController.h"


@interface KTThumbsViewController (Private)
@end


@implementation KTThumbsViewController

@synthesize dataSource = dataSource_;
- (void)dealloc {
   [scrollView_ release], scrollView_ = nil;
   
   [super dealloc];
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Make sure to set wantsFullScreenLayout or the photo
  // will not display behind the status bar.
  KTThumbsView *scrollView = [[KTThumbsView alloc] initWithFrame:CGRectMake(0, SCREEN_STATUS_BAR, SCREEN_WIDTH, SCREEN_HEIGHT_WITHOUT_STATUS_BAR)];
  [scrollView setDataSource:self];
  [scrollView setController:self];
  [scrollView setScrollsToTop:YES];
  [scrollView setScrollEnabled:YES];
  [scrollView setAlwaysBounceVertical:YES];
  [scrollView setBackgroundColor:[UIColor whiteColor]];
  
  if ([dataSource_ respondsToSelector:@selector(thumbsHaveBorder)]) {
    [scrollView setThumbsHaveBorder:[dataSource_ thumbsHaveBorder]];
  }
  
  if ([dataSource_ respondsToSelector:@selector(thumbSize)]) {
    [scrollView setThumbSize:[dataSource_ thumbSize]];
  }
  
  if ([dataSource_ respondsToSelector:@selector(thumbsPerRow)]) {
    [scrollView setThumbsPerRow:[dataSource_ thumbsPerRow]];
  }
  
  
  // Retain a reference to the scroll view.
  scrollView_ = scrollView;
  [scrollView_ retain];
  
  // Set main view to the scroll view.
  [self.view addSubview:scrollView_];
  
  // Release the local scroll view reference.
  [scrollView release];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewWillAppear:(BOOL)animated {
  // The first time the view appears, store away the current translucency so we can reset on pop.
  if (!viewDidAppearOnce_) {
    viewDidAppearOnce_ = YES;
  }
  // Then ensure translucency to match the look of Apple's Photos app.

  [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
  // Restore old translucency when we pop this controller.
  [super viewWillDisappear:animated];
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)willLoadThumbs {
   // Do nothing by default.
}

- (void)didLoadThumbs {
   // Do nothing by default.
}

- (void)reloadThumbs {
   [self willLoadThumbs];
   [scrollView_ reloadData];
   [self didLoadThumbs];
}

- (void)setDataSource:(id <KTPhotoBrowserDataSource>)newDataSource {
   dataSource_ = newDataSource;
   [self reloadThumbs];
}

- (void)didSelectThumbAtIndex:(NSUInteger)index {
   KTPhotoScrollViewController *newController = [[KTPhotoScrollViewController alloc] 
                                                        initWithDataSource:dataSource_ 
                                                  andStartWithPhotoAtIndex:index];
  
   [[self navigationController] pushViewController:newController animated:YES];
   [newController release];
}


#pragma mark -
#pragma mark KTThumbsViewDataSource

- (NSInteger)thumbsViewNumberOfThumbs:(KTThumbsView *)thumbsView
{
   NSInteger count = [dataSource_ numberOfPhotos];
   return count;
}

- (KTThumbView *)thumbsView:(KTThumbsView *)thumbsView thumbForIndex:(NSInteger)index
{
   KTThumbView *thumbView = [thumbsView dequeueReusableThumbView];
   if (!thumbView) {
      thumbView = [[[KTThumbView alloc] initWithFrame:CGRectZero] autorelease];
      [thumbView setController:self];
   }

   // Set thumbnail image.
   if ([dataSource_ respondsToSelector:@selector(thumbImageAtIndex:thumbView:)] == NO) {
      // Set thumbnail image synchronously.
      UIImage *thumbImage = [dataSource_ thumbImageAtIndex:index];
      [thumbView setThumbImage:thumbImage];
   } else {
      // Set thumbnail image asynchronously.
      [dataSource_ thumbImageAtIndex:index thumbView:thumbView];
   }
   
   return thumbView;
}


@end
